package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.PestPageRequest;
import com.example.springboot.controller.request.SavePestRequest;
import com.example.springboot.entity.Pest;
import com.example.springboot.service.IPestImageService;
import com.example.springboot.service.IPestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/pest")
public class PestController {

    @Autowired
    private IPestService pestService;

    @Autowired
    private IPestImageService pestImageService;


    // 保存 pest 数据，返回生成的 id
    @PostMapping("/save")
    public Result save(@RequestBody SavePestRequest savePestRequest) {
        Integer pestId = pestService.save(savePestRequest); // 调用已有的 save 方法，返回生成的 pestId
        return Result.success(pestId);
    }

    // 更新 pest
    @PutMapping("/update")
    public Result update(@RequestBody SavePestRequest savePestRequest) {
        pestService.update(savePestRequest);
        return Result.success(savePestRequest);
    }

    /**
     * 删除 pest
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        pestService.deleteById(id);
        pestImageService.deleteByPestId(id);
        return Result.success();
    }

    /**
     * 根据 id 查询 pest
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Pest pest = pestService.getById(id);
        return Result.success(pest);
    }

    /**
     * 查询 pest 列表
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<Pest> list = pestService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     * @param pageRequest
     * @return
     */
    @GetMapping("/page")
    public Result page(PestPageRequest pageRequest) {
        return Result.success(pestService.pageWithCover(pageRequest));
    }

}
